package com.punai.config.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.constant.Constants;
import com.punai.common.core.domain.CommonConstants;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.SecurityUtils;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.domain.ConfigInventoryLib;
import com.punai.config.mapper.ConfigInventoryLibMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.config.mapper.ConfigInventoryChapterMapper;
import com.punai.config.domain.ConfigInventoryChapter;
import com.punai.config.service.IConfigInventoryChapterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 清单库Service业务层处理
 *
 * @author zxw
 * @date 2023-03-22
 */
@Service
public class ConfigInventoryChapterServiceImpl extends ServiceImpl<ConfigInventoryChapterMapper,ConfigInventoryChapter> implements IConfigInventoryChapterService
{
    @Autowired
    private ConfigInventoryChapterMapper configInventoryChapterMapper;

    @Autowired
    private ConfigInventoryLibMapper configInventoryLibMapper;
    /**
     * 查询清单库
     *
     * @param id 清单库主键
     * @return 清单库
     */
    @Override
    public ConfigInventoryChapter selectConfigInventoryChapterById(String id)
    {
        return configInventoryChapterMapper.selectConfigInventoryChapterById(id,SecurityUtils.getCompanyId());
    }

    /**
     * 查询清单库列表
     *
     * @param configInventoryChapter 清单库
     * @return 清单库
     */
    @Override
    public List<ConfigInventoryChapter> selectConfigInventoryChapterList(ConfigInventoryChapter configInventoryChapter)
    {
        return configInventoryChapterMapper.selectConfigInventoryChapterList(configInventoryChapter);
    }

    /**
     * 新增清单库
     *
     * @param configInventoryChapter 清单库
     * @return 结果
     */
    @Override
    public int insertConfigInventoryChapter(ConfigInventoryChapter configInventoryChapter)
    {
        String pid = configInventoryChapter.getPid();
        String pids = "0";
        if (StrUtil.isEmpty(pid)) {
            pid = CommonConstants.DEF_PID;
            configInventoryChapter.setChapterType(PnConfigConstants.CHAPTER_ZY_TYPE);
        } else {
            ConfigInventoryChapter parent = getById(pid);
            if (StrUtil.equals(PnConfigConstants.CHAPTER_ZY_TYPE,parent.getChapterType())) {
                configInventoryChapter.setChapterType(PnConfigConstants.CHAPTER_FBGC_TYPE);
            }
            pids = parent.getPids() + Constants.SEPARATOR + pid;
        }
        configInventoryChapter.setPid(pid);
        configInventoryChapter.setPids(pids);
        Integer maxOrderNum = getMaxOrderNum(configInventoryChapter.getPid(),configInventoryChapter.getCompanyId());
        configInventoryChapter.setOrderNum(maxOrderNum + 1);
        return configInventoryChapterMapper.insert(configInventoryChapter);
    }

    private Integer getMaxOrderNum(String pid,Long companyId) {
        return configInventoryChapterMapper.selectMaxOrderNum(pid,companyId);
    }

    /**
     * 修改清单库
     *
     * @param configInventoryChapter 清单库
     * @return 结果
     */
    @Override
    public int updateConfigInventoryChapter(ConfigInventoryChapter configInventoryChapter)
    {
        return configInventoryChapterMapper.update(configInventoryChapter,
                Wrappers.<ConfigInventoryChapter>lambdaUpdate()
                        .eq(ConfigInventoryChapter::getCompanyId,configInventoryChapter.getCompanyId())
                        .eq(ConfigInventoryChapter::getId,configInventoryChapter.getId())
        );
    }

    /**
     * 批量删除清单库
     *
     * @param ids 需要删除的清单库主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteConfigInventoryChapterByIds(String[] ids)
    {
        String id = ids[0];
        Long aLong = configInventoryChapterMapper.selectCount(
                Wrappers.<ConfigInventoryChapter>lambdaQuery()
                        .eq(ConfigInventoryChapter::getPid, id)
                        .eq(ConfigInventoryChapter::getCompanyId, SecurityUtils.getCompanyId())
        );
        if (aLong > 0) {
            throw new ServiceException("存在子章节!无法删除!");
        }
        configInventoryLibMapper.delete(
                Wrappers.<ConfigInventoryLib>lambdaQuery()
                        .eq(ConfigInventoryLib::getChapterId,id)
                        .eq(ConfigInventoryLib::getCompanyId, SecurityUtils.getCompanyId())
        );
        return configInventoryChapterMapper.deleteConfigInventoryChapterByIds(ids,SecurityUtils.getCompanyId());
    }


    @Override
    public List<TreeBo> treelist(ConfigInventoryChapter configInventoryChapter, Long companyId) {
        String type = configInventoryChapter.getType();
        String listNorm = configInventoryChapter.getListNorm();
        List<ConfigInventoryChapter> configInventoryChapters = configInventoryChapterMapper.selectList(
                Wrappers.<ConfigInventoryChapter>lambdaQuery()
                        .eq(ConfigInventoryChapter::getCompanyId, companyId)
                        .eq(ConfigInventoryChapter::getType, type)
                        .eq(ConfigInventoryChapter::getListNorm, listNorm)
        );
        return TreeBo.convert(configInventoryChapters);
    }
}
